'use strict';

let {Sequelize,DataTypes} = require('sequelize');

let sequelize = new Sequelize('demo','postgres','XiongYY200012152841735357***',{
    host:'47.113.121.34',
    dialect:'postgres'
});




function defineModel(name,attrs){
    let prop = {};

    prop.id = {
        type:DataTypes.BIGINT,
        primaryKey:true,
        autoIncrement:true
    };

    for(let key in attrs){
        let value = attrs[key];
        prop[key] = value;
    };

    prop.createAt = {
        type:DataTypes.BIGINT,
        allowNull:false
    };

    prop.updateAt = {
        type:DataTypes.BIGINT,
        allowNull:false
    };

    prop.version = {
        type:DataTypes.BIGINT,
        allowNull:false
    }

    prop.remarks = {
        type:DataTypes.STRING(400),
        allowNull:true
    };

    let obj = sequelize.define(name,prop,{
        timestamps:false,
        hooks:{
            beforeValidate:function(obj){
                let now = Date.now();

                if(obj.isNewRecord){
                    obj.createAt = now;
                    obj.updateAt = now;
                    obj.version = 0;
                }else{
                    obj.updateAt = now;
                    obj.version += 1;
                }
            }
        }
    });
    return obj;
}

let objs = {sequelize,DataTypes,defineModel};

module.exports = objs;